<template>
  <el-container class="container">
    <el-card>
      <h1>今日好评</h1>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-container>
</template>
<script>
import {login} from '@/api/account'
import { getBranchList } from "@/api/branch";
export default {
  data() {
    return {
      form: {
        username: '',
        password: ''
      }
    }
  },
  methods: {
    onSubmit() {
      login(this.form).then((res) => {
        let token = res.data.token
        let user = res.data.user
        sessionStorage.setItem('token', token)
        sessionStorage.setItem('user', JSON.stringify(user))
        if (user.role != 'ADMIN') {
          sessionStorage.setItem('selected_shop_id', user.shopId)
          getBranchList(user.shopId).then(res =>{
            this.$store.commit('setBranchList', res.data.data)
            sessionStorage.setItem('branch_list', JSON.stringify(res.data.data))
            sessionStorage.setItem('selected_branch_id', res.data.data[0].id)
          })
        }
        if (user.role == 'ADMIN' || user.role == 'MANAGER') { 
            this.$router.push('/overview')
        } else {
           this.$router.push('/vote/rate')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #1d3548;
}
h1 {
  text-align: center;
}
</style>
